package ru.reaperoq.dto

import kotlinx.serialization.Serializable
import ru.reaperoq.database.Database
import ru.reaperoq.schema.Location
import ru.reaperoq.schema.User

@Serializable
data class UserResponse(
    val id: Int,
    val username: String,
    val role: String,
    val createdAt: Long,
    val location: Location? = null
) {
    companion object {
        private val locationsService = Database.locationsService

        suspend fun from(user: User): UserResponse {
            return UserResponse(
                user.id,
                user.username,
                user.role.name,
                user.createdAt,
                user.locationId?.let { locationsService.read(it) }
            )
        }
    }
}
